package cn.chengpengper.nacos.api.naming.pojo;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
public class Instance implements Serializable {
    private static final long serialVersionUID = -742906310567291979L;

    private String instanceId;

    private String ip;

    private int port;

    private double weight = 1.0D;

    private boolean healthy = true;

    private boolean enabled = true;

    private boolean ephemeral = true;

    private String clusterName;

    private String serviceName;

    private Map<String, String> metadata = new HashMap<>();

    @Override
    public String toString() {
        return "Instance{" + "instanceId='" + instanceId + '\'' + ", ip='" + ip + '\'' + ", port=" + port + ", weight="
                + weight + ", healthy=" + healthy + ", enabled=" + enabled + ", ephemeral=" + ephemeral
                + ", clusterName='" + clusterName + '\'' + ", serviceName='" + serviceName + '\'' + ", metadata="
                + metadata + '}';
    }

    public String toInetAddr() {
        return ip + ":" + port;
    }

    @Override
    public boolean equals(final Object obj) {
        if (!(obj instanceof Instance)) {
            return false;
        }

        final Instance host = (Instance) obj;
        return Instance.strEquals(host.toString(), toString());
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    private static boolean strEquals(final String str1, final String str2) {
        return Objects.equals(str1, str2);
    }
}
